package yb.ecp.fast.user.web;

import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.user.infra.BasicController;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.service.MenuService;
import yb.ecp.fast.user.service.ScepterService;
import yb.ecp.fast.user.service.VO.AuthVO;
import yb.ecp.fast.user.service.VO.MenuAuthListVO;
import yb.ecp.fast.user.service.VO.MenuDisplayVO;

import java.util.List;

/**
 * Created by john on 2017/11/25.
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BasicController {
    @Autowired
    ScepterService scepterService;
    @Autowired
    MenuService menuService;

    @RequestMapping(value = "/authorized", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "查询按钮集合（树形结构）")
    public ActionResult authorizedList(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        List<MenuDisplayVO> menuDisplayVOList = scepterService.getShownMenuByUser(userId);
        return actionResult(menuDisplayVOList);
    }

    @RequestMapping(value = "/listAuthorized", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "查询按钮集合")
    public ActionResult listAuthorized(@RequestHeader(name = "x-user-id", required = false) String userId, @RequestHeader("x-from-site") Integer site) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        List<MenuDisplayVO> menuDisplayVOList = scepterService.listShownMenuByUser(userId, site);
        return actionResult(menuDisplayVOList);
    }

    @RequestMapping(value = "/shown", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = false)
    public ActionResult shownList(@RequestHeader(name = "x-user-id", required = false) String userId) {
        List<MenuDisplayVO> menuDisplayVOList = menuService.getShownMenuTree(userId);
        return actionResult(menuDisplayVOList);
    }

    @RequestMapping(value = "/menuAuth", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = false)
    public ActionResult listAuthByMenu(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        List<MenuAuthListVO> menuAuthListVOList = menuService.getAuthListByMenu(userId);
        return actionResult(menuAuthListVOList);
    }

    @RequestMapping(value = "/listUserAuth", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult listAuthByUser(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        List<AuthVO> authVOs;
        authVOs = menuService.listAuthByUser(userId);
        return actionResult(authVOs);
    }

    @RequestMapping(value = "/listMenuBySite", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "查询权限模板配置页面所需数据")
    public ActionResult listMenuBySite(@RequestParam("site") Integer site) {
        List<MenuDisplayVO> menuDisplayVOList = menuService.listMenuBySite(site);
        return actionResult(menuDisplayVOList);
    }
}
